package cn.com.xinjinfu.pdoulcProduct.utils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONObject;

import java.util.Map;

import cn.com.xinjinfu.pdoulcProduct.engine.DataHandler;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.JSONManager;

/**
 * 登陆请求，密码输入
 * Created by android-adt on 2016/1/19.
 */
public class LoginRequestUtils {

    private final String ACTION_NAME = "跳转首页广播";
    private Activity context;
    public LoginRequestUtils(Activity context) {
        this.context = context;
    }
    /**
     * 登录请求
     */
    public void loginRequest(RequestQueue requen , String mobile,String pwd)
    {
        final Map<String, String> parameters = DataHandler.getParameters("1002");
        parameters.put("mobile", mobile);
        parameters.put("passwd", DataHandler.encrypt3DES(pwd));
        JsonObjectRequest request = new JsonObjectRequest(DataHandler.getBuildUrl(parameters),null,succeedRespon,error);
        requen.add(request);
    }
    private Response.ErrorListener error = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            DataHandler.errorHandler(context, error);
        }
    };

    private Response.Listener<JSONObject> succeedRespon = new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject jobj) {
            if(JSONManager.getError(jobj) == -1){
                Log.i("解析的User===========", jobj.toString());
                AccountManager.getInstance().saveUserInfoToFile(jobj);
                ActivityManager.getInstance().exitActivity();
                Intent mIntent = new Intent(ACTION_NAME);
                mIntent.putExtra("yaner", "shouye");
                //发送广播
                context.sendBroadcast(mIntent);
            }
        }
    };
}
